//
//  STCalendarKitWeakView.m
//  ZuoBiao
//
//  Created by stoneobs on 2017/7/29.
//  Copyright © 2017年 shixinyun. All rights reserved.
//

#import "STCalendarKitWeakView.h"
#import "STCalendarKitItemCollectionViewCell.h"
#import "STCalendarKitWeekManger.h"
static NSString * const STCalendarKitWeekCollectionCellIdentifier = @"STCalendarKitWeekCollectionCellIdentifier";
@interface STCalendarKitWeakView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView                                  *collectionView;

@property(nonatomic,strong)NSMutableArray<STCalendarKitDayModel*>            *datasouce;

@property(nonatomic,strong)STCalendarKitDayModel                             *selectedModel;//存放选中的model，其他被选中，则全部取消选择，只保留一个选择

@end
@implementation STCalendarKitWeakView

- (void)dealloc
{
    NSLog(@"STCalendarKitWeakView  dealloc");
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}
- (void)layoutSubviews{
    [self createData];
    [self addSubview:self.collectionView];
}
#pragma mark --构造数据
- (void)createData{
    
    [self scrollToDate:[NSDate localCurrentDate]];
}

#pragma mark --Geter And Seter
- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
        [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        flow.minimumLineSpacing = 0;
        flow.minimumInteritemSpacing = 0;
        flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flow];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[STCalendarKitItemCollectionViewCell class]
            forCellWithReuseIdentifier:STCalendarKitWeekCollectionCellIdentifier];
        
    }
    return _collectionView;
    
}
#pragma --mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datasouce.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWith, itemWith);
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    STCalendarKitItemCollectionViewCell * item = [collectionView dequeueReusableCellWithReuseIdentifier:STCalendarKitWeekCollectionCellIdentifier forIndexPath:indexPath];
    item.model = self.datasouce[indexPath.item];
    [item setButtonAction:^(UIButton * buttton){
        [weakSelf buttonAction:buttton indexPath:indexPath];
    }];
    return item;
}

#pragma --mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)buttonAction:(UIButton*)sender indexPath:(NSIndexPath*)indexPath{
    
    BOOL originIsSlected = self.selectedModel.isSelected;
    self.selectedModel.isSelected = NO;
    STCalendarKitDayModel * model = self.datasouce[indexPath.row];
    if (model == self.selectedModel) {
        //如果再次点击已被点击过的cell，那么只是切换点击和非点击状态，而不是统一变为非点击。
        self.selectedModel.isSelected = !originIsSlected;
    }
    self.selectedModel = model;

    [self.collectionView reloadData];
    
    

}
#pragma mark --Public Method
- (void)scrollToDate:(NSDate *)date{

    //初始化上下 10周 共20周的数据
    self.datasouce = [NSMutableArray new];
    
    NSArray * lastArray = [[STCalendarKitWeekManger defult] calculatelastWeeksWithDate:date nums:10];
    
    [self.datasouce addObjectsFromArray:lastArray];
    
    //获取接下来10周的数据
    NSArray * nextArray = [[STCalendarKitWeekManger defult] calculateNextWeeksWithDate:date nums:10];
    for (STCalendarKitDayModel * model in nextArray) {
        if ([model.date.yyyyMMddByLineWithDate isEqualToString:date.yyyyMMddByLineWithDate]) {
            model.isSelected = YES;
        }
    }
    
    [self.datasouce addObjectsFromArray:nextArray];
    
    [self.collectionView reloadData];

    [self.collectionView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width * lastArray.count/7, 0) animated:YES];
}
@end
